import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

//自定义组件
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // MaterialApp是根组件
    return MaterialApp(
      home: CheckBox(),
      theme: ThemeData(primaryColor: Colors.blue),
    );
  }
}

class CheckBox extends StatefulWidget {
  CheckBox({Key? key}) : super(key: key);

  @override
  _CheckBoxState createState() => _CheckBoxState();
}

class _CheckBoxState extends State<CheckBox> {
  var flag = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("checkbox"),
      ),
      body: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Checkbox(
                  activeColor: Colors.tealAccent,
                  value: flag,
                  onChanged: (value) {
                    setState(() {
                      this.flag = value!;
                    });
                  })
            ],
          ),
          Row(
            children: [Text(this.flag ? "选中" : "未选中")],
          ),
          Divider(),
          CheckboxListTile(
            value: this.flag,
            selected: this.flag,
            onChanged: (v) {
              setState(() {
                this.flag = v!;
              });
            },
            title: Text("标题"),
            subtitle: Text("二级标题"),
            secondary: Icon(Icons.ac_unit),
          )
        ],
      ),
    );
  }
}
